[][src]Crate binary_heap_plus

This crate provides BinaryHeap which is backward-compatible with std::collections::BinaryHeap.

Constructers

Dedicated methods to create different kind of heaps

  • BinaryHeap::new() creates a max heap.
  • BinaryHeap::new_min() creates a min heap.
  • BinaryHeap::new_by() creates a heap sorted by the given closure.
  • BinaryHeap::new_by_key() creates a heap sorted by the key generated by the given closure.

Generic methods to create different kind of heaps from initial vec data.

  • BinaryHeap::from_vec(vec)
  • BinaryHeap::from_vec_cmp(vec, cmp)
use binary_heap_plus::*;
 
// max heap
let mut heap: BinaryHeap<i32> = BinaryHeap::from_vec(vec![1,5,3]);
assert_eq!(heap.pop(), Some(5));
 
// min heap
let mut heap: BinaryHeap<i32, MinComparator> = BinaryHeap::from_vec(vec![1,5,3]);
assert_eq!(heap.pop(), Some(1));
 
// custom-sort heap
let mut heap = BinaryHeap::from_vec_cmp(vec![1,5,3], FnComparator(|a: &i32, b: &i32| a.cmp(b)));
assert_eq!(heap.pop(), Some(5));
 
// custom-key heap
let mut heap = BinaryHeap::from_vec_cmp(vec![6,3,1], KeyComparator(|k: &i32| k % 4));
assert_eq!(heap.pop(), Some(3));
 
// TIP: How to reuse a comparator
let mod4_comparator = KeyComparator(|k: &_| k % 4);
let mut heap = BinaryHeap::from_vec_cmp(vec![6,3,1], mod4_comparator);
assert_eq!(heap.pop(), Some(3));

Structs

BinaryHeap

A priority queue implemented with a binary heap.

Drain

A draining iterator over the elements of a BinaryHeap.

FnComparator

The comparator defined by closure

IntoIter

An owning iterator over the elements of a BinaryHeap.

Iter

An iterator over the elements of a BinaryHeap.

KeyComparator

The comparator ordered by key

MaxComparator

For T that implements Ord, you can use this struct to quickly set up a max heap.

MinComparator

For T that implements Ord, you can use this struct to quickly set up a min heap.

PeekMut

Structure wrapping a mutable reference to the greatest item on a BinaryHeap.

Traits

Compare

Simpler replacement for the Ord trait. The difference is that you can define multiple sort orders on a single type T. Unlike Ord trait, Compare<T> trait can be easily implemented by providing a single function.